package com.baicaierp.baicaimp.security;

import com.baicaierp.baicaimp.po.Operator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

/**
 * @description
 * @author: fanxl
 * @date: 2017/11/16 14:08
 */
@Slf4j
@Component
public class SportUserDetailService implements UserDetailsService, SocialUserDetailsService {

//    @Autowired
//    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        User user = userService.getByUsername(username);
//        if (user == null) {
//            throw new UsernameNotFoundException("用户名不存在");
//        }
        if (username.equals("123")){
            Operator operator = new Operator();
            operator.setCode(username);
            String password = passwordEncoder.encode("123");
            log.info("password:{}", password);
            operator.setPassword(password);
            return operator;
        }
        return null;
    }

    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
//        User user = userService.getById(userId);
//        if (user == null) {
//            throw new UsernameNotFoundException("用户不存在");
//        }
        if (userId.equals("1234")){
            Operator operator = new Operator();
            return operator;
        }
        return null;
    }
}
